﻿''' <summary>
''' This is a control which represents a collection of values in an INI section.
''' </summary>
''' <remarks></remarks>
Public Class Section

    Private _KeyPairList As List(Of KeyValuePair)

    Private _Name As String = String.Empty

    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property

    ''' <summary>
    ''' Add a new section by name plus add
    ''' keypairs to the section.
    ''' 
    ''' The keypairs would look something like this
    ''' in the KeyPairs (String) array:
    ''' KeyPairs(0): "key1=value2"
    ''' keyPairs(1): "key2=value2"
    ''' </summary>
    ''' <param name="Section">This is then name of this section, in an INI document
    ''' this value is define like this: "[Section]".</param>
    ''' <param name="KeyPairs">This is an array of string values which represent key/value pairs.</param>
    ''' <remarks></remarks>
    Sub New(ByVal Section As String, ByVal KeyPairs As String())
        MyBase.New()
        _KeyPairList = New List(Of KeyValuePair)

        _Name = Section

        For Each KeyPairString As String In KeyPairs
            Dim KeyValuePair As New KeyValuePair(KeyPairString)


            'MsgBox(KeyValuePair.Key)

            'MyBase.Add(KeyPair)
            _KeyPairList.Add(KeyValuePair)
        Next
    End Sub

    ''' <summary>
    ''' Add a new section based on a section array.
    ''' </summary>
    ''' <param name="SectionArray">A section array is an INI section and key/value pairs based in an
    ''' array of string.
    ''' 
    ''' Here is an example of what an array might look like:
    ''' SectionArray(0): "[Section]"
    ''' SectionArray(1): "key1=value1"
    ''' SectionArray(2): "key2=value2"
    ''' </param>
    ''' <remarks></remarks>
    Sub New(ByVal SectionArray As String())
        MyBase.New()
        _KeyPairList = New List(Of KeyValuePair)
        _Name = SectionArray(0).Trim("[")
        _Name = _Name.Trim("]")

        For i As Integer = 1 To SectionArray.Length - 1
            Dim KeyValuePair As New KeyValuePair(SectionArray(i))
            _KeyPairList.Add(KeyValuePair)
        Next
    End Sub

    ''' <summary>
    ''' Add a new named blank section.
    ''' </summary>
    ''' <param name="Section">The name of the section.</param>
    ''' <remarks></remarks>
    Sub New(ByVal Section As String)
        MyBase.New()
        _KeyPairList = New List(Of KeyValuePair)
        _Name = Section
    End Sub

    Public Shadows Function toString() As String

        Dim R As String = String.Empty

        For Each Pair As KeyValuePair In _KeyPairList
            R += Pair.toString() & vbNewLine
        Next

        Return "[" & _Name & "]" & vbNewLine & R
    End Function

    Default Public Shadows ReadOnly Property Item(ByVal Key As String) As KeyValuePair
        Get
            For Each s As KeyValuePair In _KeyPairList
                If s.Key = Key Then
                    Return s
                End If
            Next

            Return Nothing
        End Get
    End Property

    Public Sub Add(ByVal KeyPair As KeyValuePair)
        _KeyPairList.Add(KeyPair)
    End Sub

    Public Sub Remove(ByVal KeyPair As KeyValuePair)
        _KeyPairList.Remove(KeyPair)
    End Sub

    Public Function toArray() As KeyValuePair()
        Return _KeyPairList.ToArray()
    End Function

End Class
